package com.etc.servlet;

import com.etc.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/11  11:52
 **/
@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        BookDao bookDao = new BookDao();
        try {
            int i = bookDao.deleteById(Integer.parseInt(id));
            if (i > 0){
                req.getRequestDispatcher("/index").forward(req,resp);
            }else{
                req.setAttribute("message","删除失败");
                req.getRequestDispatcher("/errorservlet").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("message","删除失败");
            req.getRequestDispatcher("/errorservlet").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
